package com.linkoog.devtools.intellij.ui.table.column_info.column;

import com.linkoog.devtools.intellij.ui.table.TableRowData;
import com.linkoog.devtools.intellij.ui.table.column_info.TableClickable;
import com.linkoog.devtools.intellij.ui.table.column_info.TableClickableProvider;
import lombok.Getter;
import lombok.Setter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import java.awt.*;

public class ActionColumnInfo<T extends TableRowData> extends AbstractColumnInfo<T, ActionColumnInfo.ActionCellValue<T>> {

    Icon icon;
    ActionCellValue<T> cellValue;

    public ActionColumnInfo(String columnName,@NotNull Icon icon,@NotNull TableClickable<T> tableClickable) {
        super(columnName);
        this.icon = icon;
        this.cellValue = new ActionCellValue<>();
        this.cellValue.setTableClickable(tableClickable);
    }

    @Override
    public int getWidth(JTable table) {
        return 20;
    }
    @Override
    public Class<ActionCellValue> getColumnClass() {
        return ActionCellValue.class;
    }

    @Override
    public ActionCellValue<T> valueOf(T rowData) {
        return cellValue;
    }

    @Nullable
    @Override
    public TableCellRenderer getRenderer(T rowData) {
        return new DefaultTableCellRenderer(){
            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                JButton button = new JButton("", icon);
                button.setVisible(true);
                button.setPreferredSize(new Dimension( 20,-1));
                button.setBorder(table.getBorder());
                button.setBackground(table.getBackground());
                return button;
            }
        };
    }


    @Getter @Setter
    public static class ActionCellValue<T extends TableRowData> implements TableClickableProvider<T> {

        private TableClickable<T> tableClickable;

    }


}
